
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
import routes from './routes'
import 'font-awesome/css/font-awesome.min.css'
import axios from 'axios'
axios.defaults.baseURL='http://localhost:80'
Vue.prototype.$http = axios
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

// axios.defaults.baseURL='/api'
//NProgress.configure({ showSpinner: false });

const router = new VueRouter({routes})
// 前端页面的登录拦截
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login') {
    localStorage.removeItem('logininfo');
  }
  let user = JSON.parse(localStorage.getItem('logininfo'));
  if (!user && to.path != '/login') {
    next({ path: '/login' })
  } else {
    next()
  }
})
//为了后端校验是否已经登录，只要用axios的请求都要携带token
axios.interceptors.request.use(config=>{
  let token = localStorage.getItem("token")
  if(token){
    config.headers["token"] = token;
  }
  return config;
},error => {
  Promise.reject(error)
})
//axios后置拦截器实现后端返回来之后先会来执行这里的代码
axios.interceptors.response.use(config=>{
  let data = config.data;
  if(!data.success && "noLogin"===data.message)
  {
    localStorage.removeItem("token");
    localStorage.removeItem("loginInfo");
    router.push({ path: '/login' });
  }
  return config;
},error => {
  Promise.reject(error)
})

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

